//
//  AboutViewController.m
//  location
//
//  Created by Dongxiang Xu on 2025/6/9.
//

#import "AboutViewController.h"
#import "PolicyWebViewController.h"
#import <SafariServices/SafariServices.h>

@interface AboutViewController ()

@end

@implementation AboutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = WhiteColor;
    [self initView];
}

- (void)initView
{
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(16, NAVHEIGHT+6, SCREEN_WIDTH-32, 32)];
    title.font = REAL_MEDIUM_FONT(24);
    NSString *appDisplayName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    if (appDisplayName == nil) {
        appDisplayName = [[NSBundle mainBundle] bundleIdentifier];
    }
    title.text = [NSString stringWithFormat:@"关于%@",appDisplayName];
    title.textColor = RealBlackColor;
    [self.view addSubview:title];

    NSArray *infoArray = @[@"用户协议",@"隐私政策",@"版本信息"];
    UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(16, title.maxY+16, SCREEN_WIDTH-36, 48*infoArray.count)];
    infoView.backgroundColor = RealWhiteColor;
    infoView.layer.masksToBounds = YES;
    [infoView.layer setCornerRadius:16];
    [self.view addSubview:infoView];
    for (int i = 0; i < infoArray.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, i*48, infoView.width, 48)];
        button.backgroundColor = RealWhiteColor;
        button.layer.masksToBounds = YES;
        [infoView addSubview:button];
                    
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 104, button.height)];
        label.text = infoArray[i];
        label.textColor = RealBlackColor;
        label.font = MEDIUM_FONT(15);
        [button addSubview:label];
        
        if (i==(infoArray.count-1)) {
            UILabel *sub = [[UILabel alloc] initWithFrame:CGRectMake(button.width-238, 0, 204, button.height)];
            sub.font = DEFAULT_FONT(14);
            sub.textAlignment = NSTextAlignmentRight;
            sub.textColor = GraryTooHeavyColor;
            NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
            NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
            sub.text = [NSString stringWithFormat:@"%@ (Build %@)",version,build];
            [button addSubview:sub];
        }else{
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(16, button.height-1, button.width-32, 1)];
            line.image = [UIImage imageNamed:@"line"];
            [button addSubview:line];
        }
               
        UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(button.width-32, (button.height-12)/2, 12, 12)];
        arrow.image = [UIImage imageNamed:@"arrow_right"];
        [button addSubview:arrow];
        
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (i==0) {
                PolicyWebViewController *vc = [[PolicyWebViewController alloc] initWithRelativePath:@"http://m.tanhuos.com/user_agreement.html"];
                vc.modalPresentationStyle = UIModalPresentationPageSheet;
                [self presentViewController:vc animated:YES completion:nil];
            }else if (i==1){
                PolicyWebViewController *vc = [[PolicyWebViewController alloc] initWithRelativePath:@"http://m.tanhuos.com/privacy_policy.html"];
                vc.modalPresentationStyle = UIModalPresentationPageSheet;
                [self presentViewController:vc animated:YES completion:nil];
            }else if (i==2){
                
            }
        }];
    }
}

@end
